<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传图片预览</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            padding: 20px;
            background: #f5f5f5;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .img-upload {
            display: flex;
            gap: 20px;
        }

        .img-container {
            width: 200px;
            height: 120px;
            border: 2px dashed #ddd;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            position: relative;
        }

        .img-container img {
            max-width: 100%;
            max-height: 100%;
            display: none;
        }

        .submit-btn {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .type-dependent {
            display: none;
        }
    </style>
</head>
<body>
<div class="container">
    <form id="myForm" enctype="multipart/form-data">
        <div class="form-group">
            <label>身份证照片</label>
            <div class="img-upload">
                <div class="img-container" id="frontContainer">
                    <span>正面照片</span>
                    <img id="frontPreview">
                    <input type="file" name="file" accept="image/*" style="display:none">
                </div>
                <div class="img-container" id="backContainer">
                    <span>背面照片</span>
                    <img id="backPreview">
                    <input type="file" name="file" accept="image/*" style="display:none">
                </div>
            </div>
        </div>
        <button type="submit" class="submit-btn">提交</button>
    </form>
</div>
</body>
<script>
    // 图片上传预览
    document.querySelectorAll('.img-container').forEach(container => {
        const input = container.querySelector('input[type="file"]');
        const img = container.querySelector('img');
        const span = container.querySelector('span');

        //点击img-container,触发input的click()事件,就是文件上传
        container.addEventListener('click', () => input.click());

        //文件点击上传后,触发事件 e 就是event
        input.addEventListener('change', (e) => {
            const file = e.target.files[0];  //file就是当前上传的文件
            //console.log(file);

            //方式一
            /*if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    img.src = e.target.result;
                    img.style.display = 'block';
                    span.style.display = 'none';
                };
                //readAsDataURL(file) 会将文件内容转换为 Base64 格式的 Data URL
                reader.readAsDataURL(file);
            }*/

            //方式二 start
            // 使用 URL.createObjectURL 生成临时 URL 适合大文件，生成临时 URL
            //将 file 对象转换成一个临时的 Blob URL
            const objectURL = URL.createObjectURL(file);
            img.src = objectURL;
            img.style.display = 'block';
            span.style.display = 'none';

            // 释放对象 URL，避免内存泄漏
            img.onload = () => {
                URL.revokeObjectURL(objectURL);
            };
            //方式二 end
        });
    });
</script>
</html>